<?php

		
	/* Indexed column (used for fast and accurate table cardinality) */
	$sIndexColumn = "id";
	/* DB table to use */
	$sTable = "processos";
	$sTableAux = " 
		LEFT OUTER
		JOIN tipoprocessos ON tipoprocessos.id = processos.tipoprocesso_id
		LEFT OUTER
		JOIN usuarios ON usuarios.id = processos.usuario_id 
	";

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++	
	/* 
	 * Paging
	 */
	$sLimit = "";
	if ( isset( $_GET['iDisplayStart'] ) && $_GET['iDisplayLength'] != '-1' )
	{
		$sLimit = "LIMIT ".intval( $_GET['iDisplayStart'] ).", ".
			intval( $_GET['iDisplayLength'] );
	}
	
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++	
	/*
	 * Ordering
	 */
	$sOrder = "";
	if ( isset( $_GET['iSortCol_0'] ) )
	{
		$sOrder = "ORDER BY  ";
		for ( $i=0 ; $i<intval( $_GET['iSortingCols'] ) ; $i++ )
		{
			if ( $_GET[ 'bSortable_'.intval($_GET['iSortCol_'.$i]) ] == "true" )
			{
				$sOrder .= "`".$aColumns[ intval( $_GET['iSortCol_'.$i] ) ]."` ".
					($_GET['sSortDir_'.$i]==='asc' ? 'asc' : 'desc') .", ";
			}
		}
		
		$sOrder = substr_replace( $sOrder, "", -2 );
		if ( $sOrder == "ORDER BY" )
		{
			$sOrder = "";
		}
	}
	
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++	
	/* 
	 * Filtering
	 * NOTE this does not match the built-in DataTables filtering which does it
	 * word by word on any field. It's possible to do here, but concerned about efficiency
	 * on very large tables, and MySQL's regex functionality is very limited
	 */
	$sWhere = "";
	if ( isset($_GET['sSearch']) && $_GET['sSearch'] != "" )
	{
		$sWhere = "WHERE (";
		for ( $i=0 ; $i<count($aColumns) ; $i++ )
		{
			if ( isset($_GET['bSearchable_'.$i]) && $_GET['bSearchable_'.$i] == "true" )
			{
				$sWhere .= "`".$aColumns[$i]."` LIKE '%".mysql_real_escape_string( $_GET['sSearch'] )."%' OR ";
			}
		}
		$sWhere = substr_replace( $sWhere, "", -3 );
		$sWhere .= ')';
	}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++	
	/* Individual column filtering */
	for ( $i=0 ; $i<count($aColumns) ; $i++ )
	{
		if ( isset($_GET['bSearchable_'.$i]) && $_GET['bSearchable_'.$i] == "true" && $_GET['sSearch_'.$i] != '' )
		{
			if ( $sWhere == "" )
			{
				$sWhere = "WHERE ";
			}
			else
			{
				$sWhere .= " AND ";
			}
			$sWhere .= "`".$aColumns[$i]."` LIKE '%".mysql_real_escape_string($_GET['sSearch_'.$i])."%' ";
		}
	}
	
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++	
	/*
	 * SQL queries
	 * Get data to display
	 */
	/*CHARSET*/
	mysql_query("SET NAMES 'utf8'");
	
	$sQuery = "
		SELECT SQL_CALC_FOUND_ROWS processos.dt_cadastro, processos.id, processos.numeroprocesso, processos.dt_processo, processos.usuario_id, processos.usuario_id_assis,processos.usuario_id_redist, processos.status,tipoprocessos.nomeprocesso, usuarios.nome_completo
		FROM   $sTable
		$sTableAux
		$sWhere
		$sOrder
		$sLimit
		";
	$rResult = mysql_query( $sQuery, $gaSql['link'] ) or fatal_error( 'MySQL Error: ' . mysql_errno() );
	
	/* Data set length after filtering */
	$sQuery = "
		SELECT FOUND_ROWS()
	";
	$rResultFilterTotal = mysql_query( $sQuery, $gaSql['link'] ) or fatal_error( 'MySQL Error: ' . mysql_errno() );
	$aResultFilterTotal = mysql_fetch_array($rResultFilterTotal);
	$iFilteredTotal = $aResultFilterTotal[0];
	
	/* Total data set length */
	$sQuery = "
		SELECT COUNT(`".$sIndexColumn."`)
		FROM   $sTable
	";
	$rResultTotal = mysql_query( $sQuery, $gaSql['link'] ) or fatal_error( 'MySQL Error: ' . mysql_errno() );
	$aResultTotal = mysql_fetch_array($rResultTotal);
	$iTotal = $aResultTotal[0];
	
	
	/*
	 * Output
	 */
	$output = array(
		"sEcho" => intval($_GET['sEcho']),
		"iTotalRecords" => $iTotal,
		"iTotalDisplayRecords" => $iFilteredTotal,
		"aaData" => array()
	);
	
	while ( $aRow = mysql_fetch_array( $rResult ) )
	{
		$row = array();
		for ( $i=0 ; $i < count($aColumns) ; $i++ )
		{
			if($i == 0){
				$id = $aRow[$aColumns[$i]];
				$row[] = $aRow[$aColumns[$i]];
			}elseif($i == 3){
				$row[] = usatoBr($aRow[$aColumns[$i]]);
			}else{
				$row[] = $aRow[$aColumns[$i]];
			}
		}
	
		$row[] = '<a href=javascript:void() onclick=btn_acao("formCad_paciente.php",'.$id.');><img src=../../images/edit.png width=16 height=16 title="Editar Paciente" ><a><a href=javascript:void() onclick=btn_acao("historico_paciente.php",'.$id.');><img src=../../images/historico.png width=16 height=16 title="Historico Paciente" ><a><div style=float:right;text-align:right;width:50px;><a href=javascript:void() onclick=btn_acao("../laudos/formCad_laudo.php",'.$id.');><img src=../../images/add_laudo2.png width=16 height=16 title="Iniciar Laudo" ><a><div>';

		$output['aaData'][] = $row;
	}
	
	echo json_encode( $output );
?>